package telcoGUI.actionListener;

import java.awt.event.ActionEvent;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import swa2012.telco.broker.beans.Client;
import swa2012.telco.broker.beans.Message;
import telcoGUI.userInterface.ClientBean;
import telcoGUI.userInterface.TelcoClientGUI;

public class SendButtonListener extends TelcoActionListener
{
	public SendButtonListener(TelcoClientGUI telcoGUI)
	{
		super(telcoGUI);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		ClientBean c 		= (ClientBean) this.telcoGUI.getDestinationComboBox().getSelectedItem();
		Client destination	= c.getClient();
		
		System.out.println("sender: " + this.telcoGUI.getTelcoClient().getClient().getID() + "@" + this.telcoGUI.getTelcoClient().getClient().getProvider().getID());
		System.out.println("chosen destination: " + destination.getID() + "@" + destination.getProvider().getID());
		
		if (destination != null) {
			String text = this.telcoGUI.getWriteMessageTextArea().getText();
			
			// Timestamp erzeugen
			Calendar calendar = Calendar.getInstance();
			Date currentTimestamp = new Timestamp(calendar.getTime().getTime());
			
			Message msg = new Message();
			msg.setReceiver(destination);
			msg.setSender(this.telcoGUI.getTelcoClient().getClient());
			msg.setText(text);
			msg.setTimestamp(currentTimestamp.getTime());
			
			this.telcoGUI.getTelcoClient().sendMessage(msg);
			this.telcoGUI.getWriteMessageTextArea().setText("");
		}
	}
}
